<!--
 * @Author: Cenven
 * @Date: 2021-07-30 15:04:46
 * @Description: 北京泓深教育发展有限公司
 * @Descripttion: 
 * @version: @Lastest
 * @LastEditTime: 2021-07-30 17:07:25
 * @CopyRight: 北京泓深教育发展有限公司
-->
<template>
  <div class="page_comment">
    <div class="comment" v-if="!sendSuccess">
      <div class="comment_hd">
        <div class="hd_left">
          <img :src="course_obj.iconPath" alt="">
        </div>
        <div class="hd_right">
          <p class="course_name">{{course_obj.courseName}}</p>
          <p class="course_info">{{ course_obj.goal }}</p>
        </div>
      </div>
      <div class="comment_content">
        <div class="comment_content_item">
          <span class="content_item_left">课程评分</span>
          <div class="content_item_right">
            <Rate v-model="comment_obj.rate" />
            <span class="star_num">{{comment_obj.rate}}星</span>
            <span class="star_tips">{{comment_obj.rate | formatRate}}</span>
          </div>
        </div>
        <div class="comment_content_item">
          <span class="content_item_left">学习感受</span>
          <div class="content_item_right">
            <Input v-model="comment_obj.content" :maxlength="500" :rows="10" type="textarea" placeholder="课程怎么样？快来说说学习感受吧" />
          </div>
        </div>
        <Button type="primary" style="margin-left:80px;margin-bottom:40px;" @click="sendComment">发表评价</Button>
      </div>
    </div>
    <div class="comment_success" v-else>
      <Icon type="md-checkmark-circle" size="100" color="#19be6b"/>
      <p class="success_text">发表成功</p>
      <p class="success_tips">系统将在{{
            seconds
          }}秒内自动跳转，如若未进行跳转，请<span @click="$router.replace('/coursedetail?id=' + course_id)">点击跳转</span></p>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import { getCourse,saveCourseComment } from "~/api/course";
export default {
  name: "page_comment",
  components: {},
  data() {
    return {
      course_obj: {},
      comment_obj: {
        rate:5,
        content:"",
      },
      seconds:5,
      sendSuccess:false,
      interval:null,
    };
  },
  computed: {
    ...mapState({
      user_info(state) {
        return state.user_info;
      }
    }),
    course_id() {
      return this.$route.query.id;
    },
  },
  watch: {},
  filters:{
    formatRate(val){
      switch (val) {
        case 1:
          return "差评！课程太差了，我要吐槽！";
        case 2:
          return "勉强中评！课程不满意";
        case 3:
          return "中评！课程一般";
        case 4:
          return "好评！课程还不错，继续保持！";
        case 5:
          return "五星好评！课程非常棒，点赞！";
        default:
          return "";
      }
    }
  },
  methods: {
    ...mapMutations(["SET_IS_LOADING"]),
    async sendComment(){
      this.SET_IS_LOADING(true);
      let params = {
        ...this.comment_obj,
        courseId:this.course_id
      }
      let result = await saveCourseComment(params);
      if (result && result.data && result.data.code == 200) {
        this.SET_IS_LOADING(false);
        window.scrollTo(0,0);
        this.sendSuccess = true;
        this.cuntDown();
      }else if (result && result.data && result.data.code == 1001) {
        this.SET_IS_LOADING(false);
        this.$Message.error(result.data.msg);
      } else {
        this.SET_IS_LOADING(false);
        this.$Message.error("发表失败！");
      }
    },
    cuntDown() {
      this.interval = setInterval(() => {
        this.seconds--;
        if (this.seconds <= 1) {
          this.seconds = 1;
          clearInterval(this.interval);
          this.$router.replace('/coursedetail?id=' + this.course_id);
        }
      }, 1000);
    },
    async getData() {
      this.SET_IS_LOADING(true);
      let result = await getCourse({
        id: this.course_id
      });
      if (result && result.data && result.data.code == 200) {
        const { data } = result.data;
        if (data && data.rows && data.rows.length > 0) {
          this.course_obj = data.rows[0];
        }
      } else {
        this.$Message.error(
          this.$t("locale.pc.mycourse.coursebuy.t7") + result &&
            result.data &&
            result.data.msg
        );
      }
      this.SET_IS_LOADING(false);
    }
  },
  created() {
    this.getData();
  },
  mounted() {}
};
</script>

<style lang="scss">
.page_comment{
  .ivu-rate{
    font-size: 24px;
  }
}

</style>
<style lang="scss" scoped>
.page_comment {
  min-height: 500px;
  .comment{
    margin: 0px auto;
    width: 750px;
    .comment_hd{
      padding: 40px 0;
      display: flex;
      justify-content: space-between;
      .hd_left{
        width: 220px;
        height: 146.6666px;
        img{
          display: block;
          width: 100%;
          height: 100%;
        }
      }
      .hd_right{
        width: 500px;
        .course_name{
          font-size: 22px;
          margin-bottom: 10px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          margin-top: 4px;
        }
        .course_info{
          font-size: 16px;
          color: #666;
          line-height: 1.4;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 4;
          overflow: hidden;
        }
      }
    }
    .comment_content{
      font-size: 14px;
      .comment_content_item{
        margin-bottom: 20px;
        .content_item_left{
          float: left;
          line-height: 30px;
          font-size: 16px;
        }
        .content_item_right{
          position: relative;
          margin-left: 80px;
          font-size: 14px;
          line-height: 28px;
          vertical-align: middle;
          .star_num{
            margin:  0 10px 0 20px;
            color: #ffb200;
            line-height: 30px;
          }
          .star_tips{
            font-size: 14px;
            line-height: 30px;
          }
        }
      }
    }
  }
  .comment_success{
    padding: 50px;
    text-align: center;
    .success_text{
      font-size: 30px;
      padding: 10px 0;
    }
    .success_tips{
      font-size: 16px;
      span{
        cursor: pointer;
        color: var(--theme-color);
        &:hover{
          text-decoration: underline;
          color: var(--theme-color);
        }
      }
    }
  }
}
</style>
